<?php

/**
 * main actions.
 *
 * @package    UniversalBackend
 * @subpackage main
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class mainActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  private function createPageContainer($title, $metaTitle, $metaKeywords, $metaContent)
  {
      
  }  
    
  public function executeIndex(sfWebRequest $request) {
      if(!$request->getParameter('route'))
      {
          $request->setParameter('route', 'index');
      }
      $page = PagesTable::getInstance()->findOneBy('route', $request->getParameter('route'));
      if($page)
      {
          $this->page = $page;
          //$this->createPageContainer($page->getTitle(), $page->getMetaTitle(), $page->getMetaKeywords(), $page->getMetaContent());
      }
      else
      {
          $this->redirect('main/index');
      }
    }
    
    
   public function executeProducts(sfWebRequest $request)
   {
       $query = Doctrine_Query::create()->from('product');
       $cattegory = $request->getParameter('cattegory') != NULL;
       if($cattegory)
       {
           $query->where('cattegory_id =?' . $request->getParameter('cattegory'));
       }
       if($request->getParameter('language'))
       {
           if($cattegory)
           {
               $query->andWhere('language_id =? ' , $request->getParameter('language'));
           }
           else
           {
               $query->where('language_id =? ' , $request->getParameter('language'));
           }
       }
       $product = $query->execute();
       if($product->count() == 1)
       {
           $request->setParameter('product', $product->getFirst()->getId());
           $this->redirect('main/product?product=' . $product->getFirst()->getId());
       }
       else if($product->count() > 1)
       {
           $this->products = $product;
       }
       else
       {
           $this->redirect('main/index?route=products404');
       }
   }

   public function executeProduct(sfWebRequest $request)
   {
       if(!$request->getParameter('product'))
       {
           $this->redirect('main/products');
       }
       $product = ProductTable::getInstance()->findOneBy('id', $request->getParameter('product'));
       if(!$product)
       {
           $this->redirect('main/products');
       }
       $this->product = $product;
   }
}
